(function($){
	$.validator.setDefaults({
		errorElement: 'div', 
    	errorPlacement: function(error, element) {
    		let item = element.parents('.item').addClass('error_item');
        	error.addClass('error_tip').appendTo(item);
        	return false;
    	},
    	success: function(element) {
    		let item = element.parents('.item').removeClass('error_item');
        	return false;
    	},
    	onkeyup: null,
    	onfocusout: function(element) {
            if(this.element(element)) {
            	$(element).parents('.item').removeClass('error_item')
            } else {
            	$(element).parents('.item').addClass('error_item');
            }
            return false;
        }
	});
	fun = $.fn.validate;
	$.fn.validate = function() {
		var formEle = $(this).addClass('form_list_validate');
		var form = fun.apply(formEle, arguments);
		var rules = form.settings.rules;
		$.each(rules, function(name, item) {
			if(item.required) {
				formEle.find('[for=' + name + ']').prepend('<i>*</i>');
			}
		});
	}
})(jQuery);
(function(){
	$.validator.addMethod("phone", function(value, element) {
	     var length = value.length;
	     var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
	     return this.optional(element) || (length == 11 && mobile.test(value));
	}, "请正确填写您的手机号码");

	$.validator.addMethod("salary", function(value, element, params) {
		let reg = /^[1-9]\d*$/;
		if(this.optional(element)) {
			return true;
		}
		if(value == '面议'){
			return true;
		}
		if(reg.test(value)) {
			return true;
		}
		value = value.replace('-', '-');
		let arr = value.split('-');
		if(arr.length != 2) {
			return false;
		}
		if(!reg.test(arr[0]) || !reg.test(arr[1])) {
			return false;
		}
		if(parseInt(arr[1]) <= parseInt(arr[0])) {
			return false;
		}
		return true;
	}, "薪资待遇不合法");

	$.validator.addMethod("labels", function(value, element, params){
		if(this.optional(element)) {
			return true;
		}
		value = value.replaceAll('，', ',');
		let arr = value.split(',');
		let length = arr.length;
		for(let i = 0; i < length; i++) {
			if(arr[i].length > 10) {
				return false;
				break;
			}
		}
		return true;
	});

	$.validator.addMethod("qq", function(value, element, params){
		if(this.optional(element)) {
			return true;
		}
		const qqRegex = /^[1-9][0-9]{4,20}$/;
    	return qqRegex.test(value);
	}, 'qq号不合法');

	$.validator.addMethod("double_required", function(value, element, params){
		let endTime = $('#' + $(element).data('to')).val();
		if(!value || !endTime) {
			return false;
		}
		return true;
	}, '时间段不能为空');

	$.validator.addMethod("timeslot", function(value, element, params){
		let endTime = $('#' + $(element).data('to')).val();
		let arr1 = value.split('-');
		let arr2 = endTime.split('-');
		if(parseInt(arr1[0], 10) > parseInt(arr2[0], 10)) {
			return false;
		}
		if(parseInt(arr1[0], 10) == parseInt(arr2[0], 10)) {
			if(parseInt(arr1[1], 10) >= parseInt(arr2[1], 10)) {
				return false;
			}
		}
		return true;
	}, '时间段不合法');
})(jQuery);
